package com.ahui.ydd.controller;

import com.ahui.ydd.dto.YddArticleDto;
import com.ahui.ydd.Vo.YddArticleVo;
import com.ahui.ydd.common.R;
import com.ahui.ydd.entity.YddArticle;
import com.ahui.ydd.page.YddArticlePage;
import com.ahui.ydd.service.YddArticleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/ydd_article")
@Slf4j
public class YddArticleController {
    @Autowired
    private YddArticleService yddArticleService;

    /**
     * 文章分页查询
     * @param yddArticleDto
     * @return
     */
    @GetMapping("/list")
    public R<YddArticlePage> page(YddArticleDto yddArticleDto){
        //分页查询拦截器
        Page<YddArticle> pageInfo = new Page(yddArticleDto.getPage(),yddArticleDto.getLimit());
        //条件构造器
        LambdaQueryWrapper<YddArticle> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.le(StringUtils.isNotEmpty(yddArticleDto.getStart_time()),YddArticle::getCreateDate,yddArticleDto.getStart_time());
        queryWrapper.ge(StringUtils.isNotEmpty(yddArticleDto.getEnd_time()),YddArticle::getCreateDate,yddArticleDto.getEnd_time());
        queryWrapper.like(StringUtils.isNotEmpty(yddArticleDto.getTitle()),YddArticle::getTitle,yddArticleDto.getTitle());
        queryWrapper.eq(yddArticleDto.getCateid()!=null,YddArticle::getCateid,yddArticleDto.getCateid());
        //查询
        Page<YddArticleVo> res=yddArticleService.pageByCondition(pageInfo,queryWrapper);
        //返回数据
        YddArticlePage yddArticlePage =new YddArticlePage();
        yddArticlePage.setCount(res.getTotal());
        yddArticlePage.setCurrentPage(res.getCurrent());
        yddArticlePage.setData(res.getRecords());
        yddArticlePage.setPageSize(res.getSize());
        yddArticlePage.setTotalPages(res.getPages());
        return R.success(yddArticlePage);
    }

    /**
     * 文章删除(数据库真实删除)
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R<String> del(int id){
        yddArticleService.removeById(id);
        return R.success("删除成功");
    }

    /**
     * 文章添加
     * @param yddArticle
     * @return
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody YddArticle yddArticle){
        yddArticleService.save(yddArticle);
        return R.success("添加文章成功");
    }

    /**
     * 文章详情(用于后台内容回显)
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public R<YddArticle> rowInfo(int id){
        YddArticle yddArticle = yddArticleService.getById(id);
        return R.success(yddArticle);
    }

    /**
     * 文章修改
     * @param yddArticle
     * @return
     */
    @PostMapping("/update")
    public R<String> update(@RequestBody YddArticle yddArticle){
        yddArticleService.updateById(yddArticle);
        return R.success("修改成功");
    }
}
